<?php

use app\assets\ApiAsset;
use app\assets\LayerAsset;
use app\assets\MaskedInputAsset;
use app\assets\TableAsset;
use app\models\KeyMap;
use app\widgets\LinkPager;
use app\widgets\ManagerTableOp;
use yii\helpers\Html;
use yii\helpers\Url;

/**
 * @var $this yii\web\View
 * @var $agentList app\models\Agent[]
 * @var $pagination yii\data\Pagination
 */

ApiAsset::register($this);
LayerAsset::register($this);
MaskedInputAsset::register($this);
TableAsset::register($this);

$this->title = '代理商列表';
$this->params['breadcrumbs'][] = '用户管理';
$this->params['breadcrumbs'][] = $this->title;
?>
<?php echo Html::beginForm('?', 'get', ['class'=>'form-inline']);?>
<div class="form-group">
    <a href="<?php echo Url::to(['/admin/user/agent-edit']);?>" class="btn btn-success btn-sm">添加</a>
</div>
<div class="form-group">
    <label for="search_username" class="sr-only">代理商ID</label>
    <?php echo Html::textInput('search_id', Yii::$app->request->get('search_id'), ['id' => 'search_id', 'class' => 'form-control', 'placeholder' => '代理商ID', 'style' => 'width: 120px;']);?>
</div>
<div class="form-group">
    <label for="search_username" class="sr-only">用户</label>
    <?php echo Html::textInput('search_username', Yii::$app->request->get('search_username'), ['id' => 'search_username', 'class' => 'form-control', 'placeholder' => '用户名', 'style' => 'width: 120px;']);?>
</div>
<div class="form-group">
    <button class="btn btn-primary btn-sm">搜索</button>
</div>
<?php echo Html::endForm();?>
<table class="table table-striped table-bordered table-hover">
    <thead>
    <tr>
        <th class="center">
            <label class="pos-rel">
                <input type="checkbox" class="ace" />
                <span class="lbl">ID</span>
            </label>
        </th>
        <th>用户名</th>
        <th>上级代理商</th>
        <th>状态</th>
        <th>预存金额</th>
        <th>创建时间</th>
        <th>操作</th>
    </tr>
    </thead>

    <tbody>
    <?php foreach ($agentList as $agent) {?>
        <tr>
            <td class="center"><label class="pos-rel"><input type="checkbox" class="ace" value="<?php echo $agent->id;?>" /><span class="lbl"><?php echo $agent->id;?></span></label></td>
            <td><?php echo Html::encode($agent->username);?></td>
            <th><?php if (empty($agent->pid)) {
                    echo '----';
                } else {
                    echo Html::a(Html::encode($agent->parent->username), ['/admin/user/agent-view', 'id' => $agent->pid]);
                }?></th>
            <td><?php echo KeyMap::getValue('agent_status', $agent->status);?></td>
            <td><?php echo $agent->money->money;?></td>
            <td><?php echo Yii::$app->formatter->asDatetime($agent->create_time);?></td>
            <td><?php echo ManagerTableOp::widget(['items' => [
                    ['icon' => 'fa fa-info-circle', 'href' => Url::to(['/admin/user/agent-view', 'id' => $agent->id]), 'btn_class' => 'btn btn-xs btn-default', 'tip' => '详情'],
                    ['icon' => 'fa fa-pencil', 'href' => Url::to(['/admin/user/agent-edit', 'id' => $agent->id]), 'btn_class' => 'btn btn-xs btn-success', 'tip' => '修改', 'color' => 'green'],
                    ['icon' => 'fa fa-cogs', 'href' => Url::to(['/admin/user/agent-config', 'id' => $agent->id]), 'btn_class' => 'btn btn-xs btn-success', 'tip' => '配置', 'color' => 'green'],
                    ['icon' => 'fa fa-edit', 'onclick' => 'modifyAgentMoney(' . $agent->id . ', \'' . $money . '\')', 'btn_class' => 'btn btn-xs btn-warning', 'tip' => '调整金额', 'color' => 'yellow'],
                    ['icon' => 'fa fa-cubes', 'href' => Url::to(['/admin/user/agent-package', 'agent_id' => $agent->id]), 'btn_class' => 'btn btn-xs btn-info', 'tip' => '套餐', 'color' => 'blue'],
                    ['icon' => 'fa fa-sticky-note-o', 'href' => Url::to(['/admin/user/commission-list', 'id' => $agent->id]), 'btn_class' => 'btn btn-xs btn-info', 'tip' => '账单', 'color' => 'blue'],
                ]]);?></td>
        </tr>
    <?php }?>
    </tbody>
</table>
<?php echo LinkPager::widget(['pagination' => $pagination]);?>
<script type="text/javascript">
    function modifyAgentMoney(id, money) {
        layer.prompt({
        formType: 0,
        value: '',
        title: '请输入要增加的金额',
    }, function(value, index, elem){
        $.getJSON('<?php echo Url::to(['/admin/user/modify-agent-money']);?>', {id: id, money: value}, function (json) {
            if (callback(json)) {
                layer.msg('金额已更新。', function () {window.location.reload();});
            }
        });
        layer.close(index);
    });
    }
</script>